import {
  HttpApi
} from '../../../model/index.js'
const httpApi = new HttpApi
Page({
  data: {
    windowHeight: 100,
    records: [],
    start: 1,
    size: 300,
    lng: "",
    lat: "",
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //设置scroll-view高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({ windowHeight: res.windowHeight });
      }
    });
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        //发送请求通过经纬度反查地址信息  
        var getAddressUrl = "https://apis.map.qq.com/ws/geocoder/v1/?location=" + res.latitude + "," + res.longitude + "&key=OTXBZ-RNX3P-VEIDG-VJDA2-26WYT-Q2BKE";
        wx.request({
          url: getAddressUrl,
          success: function (result) {
            that.setData({ address: result.data.result.address });
          }
        })
        that.getDustbin(res.longitude, res.latitude, that.data.start, that.data.size);
      },
    })
    var login = wx.getStorageSync('login')
   
  },
  goMap(e) {
    var latitude = e.currentTarget.dataset.lat;
    var longitude = e.currentTarget.dataset.lng;
    var distance = e.currentTarget.dataset.distance;
    if (distance == null) {
      wx.showModal({
        title: '提示',
        content: '回收箱未填写经纬度无法定位,请联系管理员处理',
        success: function (res) {
        }
      })
      return;
    }
    wx.navigateTo({
      url: "./nearbyRecyclingDetail/nearbyRecyclingDetail?latitude=" + JSON.stringify(latitude) + "&longitude=" + JSON.stringify(longitude),
    })
  },
  getDustbin(lng, lat, _start, _size) {
    this.loading = true
    var _this = this
    httpApi.dustbin(lng, lat, _start, _size).then(res => {
      var arr1 = _this.data.records
      var arr2 = res.data.records
      arr1 = arr1.concat(arr2)
      _this.setData({
        start: _start,
        records: arr1,
      })
    })
  },
  goMapDetail(){
    wx.navigateTo({ url: '../nearbyRecyclingMachine/nearbyRecyclingDetail/nearbyRecyclingDetail' });
  },

})
